#include "EmulatorImpl.h"
#include "cqp.h"
#include "utils.h"
#include "log.h"

namespace kq{

	EmulatorImpl::EmulatorImpl(MessageEngine& messageEngine):messageEngine_(messageEngine)
	{
	}


	EmulatorImpl::~EmulatorImpl()
	{
	}


	::grpc::Status EmulatorImpl::privateMsg(::grpc::ServerContext* context, const ::qq::message::EmulatorPrivateMsg* request, ::qq::message::Gvoid* response){
		int64_t qqid = request->qqid();
		const std::string msg = request->msg();
		UTF82GBK(gbkmsg, msg.c_str());
		LOGD("privateMsg " << gbkmsg);
		int32_t res = CQ_sendPrivateMsg(messageEngine_.authCode, qqid, gbkmsg);
		LOGD("privateMsg send " << gbkmsg);
		return grpc::Status::OK;
	}
	::grpc::Status EmulatorImpl::groupMsg(::grpc::ServerContext* context, const ::qq::message::EmulatorGroupMsg* request, ::qq::message::Gvoid* response){
		int64_t groupid = request->groupid();
		const std::string msg = request->msg();
		UTF82GBK(gbkmsg, msg.c_str());
		LOGD("groupMsg " << gbkmsg);
		int32_t res = CQ_sendGroupMsg(messageEngine_.authCode, groupid, gbkmsg);
		LOGD("groupMsg send " << gbkmsg);
		return grpc::Status::OK;
	}
	::grpc::Status EmulatorImpl::discussMsg(::grpc::ServerContext* context, const ::qq::message::EmulatorDiscussMsg* request, ::qq::message::Gvoid* response){
		int64_t discussid = request->discussid();
		const std::string msg = request->msg();
		UTF82GBK(gbkmsg, msg.c_str());
		LOGD("discussMsg " << gbkmsg);
		int32_t res = CQ_sendPrivateMsg(messageEngine_.authCode, discussid, gbkmsg);
		LOGD("discussMsg send " << gbkmsg);
		return grpc::Status::OK;
	}

	::grpc::Status EmulatorImpl::getLoginQQ(::grpc::ServerContext* context, const ::qq::message::Gvoid* request, ::qq::message::Gint64* response){
		response->set_value(messageEngine_.qqId);
		return grpc::Status::OK;
	}
	::grpc::Status EmulatorImpl::getLoginNick(::grpc::ServerContext* context, const ::qq::message::Gvoid* request, ::qq::message::Gstring* response){
		GBK2UTF8(utf8msg, messageEngine_.qqName.c_str());
		response->set_value(utf8msg);
		return grpc::Status::OK;
	}

}